<?php
/**
 *
 * @Author nikm $
 *
 * @Date 2011-12-28 $
 */

if(!defined('FROM_INDEX')) exit('Invalid request');

require ROOT_PATH.'module/admin/master.mod.php';

class moduleObj extends masterObj
{
	function __construct($request)
	{
		parent::__construct($request);

		$this->excute();
	}

	function excute()
	{
		ob_start();

		switch($this->act)
		{
			case 'menu':
				$this->menu();
				break;
			case 'home':
				$this->home();
				break;
			case 'help':
				$this->help();
				break;
			default:
				$this->main();
		}

		$body = ob_get_clean();

		$this->showBody($body);
	}

	private function main()
	{
		if($this->user['id'] < 1)
		{
			// TODO, for test
			//$this->messager("Please <a href='admin.php?m=login'>login</a> first.", null);
		}
			
		$menuList = $this->menu();
		include template('admin/index');
	}

	private function menu()
	{
		$default_open = true;
		$open_onlyone = false;
		$open_list = isset($this->request['open']) ? explode('_', $this->request['open']) : '';

		require ROOT_PATH.'config/menu_list_admin.php';

		foreach ($menu_list as $_key=>$_menu)
		{
			if(isset($_menu['sub_menu_list']) && check_array($_menu['sub_menu_list']))
			{
				foreach ($_menu['sub_menu_list'] as $_sub_key=>$_sub_menu)
				{
					if(strpos($_sub_menu['link'],":\/\/")!==false)continue;

					preg_match("~m=([^&\x23]+)&?(a=([^&\x23]*))?~", $_sub_menu['link'],$match);
						
					if(is_array($match) && count($match) == 4)
					{
						list(,$_mod,,$_code) = $match;
					}
				}
			}
		}

		$all_open_list = array_keys($menu_list);
		if($default_open && isset($this->request['open'])==false)
		{
			$open_list = $all_open_list;
		}

		foreach($menu_list as $key=>$menu)
		{
			if ($key == 1)
			{
				foreach ($menu_list as $_menu_list_s)
				{
					foreach((array)$_menu_list_s['sub_menu_list'] as $menu_s)
					{
						if($menu_s['shortcut'])
						{
							$menu['sub_menu_list'][] = $menu_s;
						}
					}
				}
			}

			if(empty($menu['sub_menu_list']))continue;

			$menu_tmp_list[$key]=$menu;

			if(in_array($key,$open_list)!=false)
			{
				$menu_tmp_list[$key]['img']='minus';
				$open_list_tmp=$open_list;
				unset($open_list_tmp[array_search($key, $open_list_tmp)]);
			}
			else
			{
				$menu_tmp_list[$key]['img']='plus';
				$menu_tmp_list[$key]['sub_menu_list']=array();
			}

			if(isset($menu['sub_menu_list']))
			{
				$menu_tmp_list[$key]['link']="?mod=index&code=menu";
				$menu_tmp_list[$key]['target']="";
			}
			else
			{
				$menu_tmp_list[$key]['target'] = 'target="main"';
			}
		}

		$menu_list = $menu_tmp_list;

		return $menu_list;
	}

	private function home()
	{
		include ROOT_PATH.'config/menu_list_admin.php';
		$shorcut_list = array();
		foreach($menu_list as $_menu_list)
		{
			foreach((array)$_menu_list['sub_menu_list'] as $menu)
			{
				if($menu['shortcut'])
				{
					$shortcut_list[$_menu_list['title']][] = $menu;
				}
			}
		}

		$item_list=array(
			"<b>影片数</b>"=>"movie",
			"<b>影评数</b>"=>"movie_review",
			"<b>注册会员数</b>"=>"user",
			"<b>在线人数</b>"=>"session",
		);

		// movie count
		if(false === ($movie_count = cache_get('movie_count')))
		{
			$sql = "select count(1) from `".TABLE_PREFIX."movie` where `is_delete` = '0'";
			$movie_count = DBFunction::getInstance()->getOne($sql);
			cache_set('movie_count', $movie_count, 18000);
		}
		$statistic['movie'] = $movie_count;

		// movie review count
		if(false === ($movie_review_count = cache_get('movie_review_count')))
		{
			$sql = "select count(1) from `".TABLE_PREFIX."movie_review` where `is_delete` = '0'";
			$movie_review_count = DBFunction::getInstance()->getOne($sql);
			cache_set('movie_review_count', $movie_review_count, 18000);
		}
		$statistic['review'] = $movie_review_count;

		// user count
		if(false === ($user_count = cache_get('user_count')))
		{
			$sql = "select count(1) from `".TABLE_PREFIX."user`";
			$user_count = DBFunction::getInstance()->getOne($sql);
			cache_set('user_count', $user_count, 18000);
		}
		$statistic['user'] = $user_count;

		if(false === ($data_length = cache_get('db_data_length')))
		{
			$sql = "show table status from `".DB_NAME."` like '".TABLE_PREFIX."%'";
			$results = DBFunction::getInstance()->getAll($sql);
			$data_length = 0;
			if(check_array($results))
			{
				foreach($results as $row)
				{
					$data_length += $row['Data_length'] + $row['Index_length'];
				}
			}

			if($data_length>0)
			{
				$data_length = size_convert($data_length);
				cache_set('db_data_length', $data_length);
			}
		}

		$sql = "SELECT count(1) FROM `" .TABLE_PREFIX."sessions`";
		$results = DBFunction::getInstance()->getOne($sql);
		$statistic['session'] = $results;

		include template('admin/home');
	}

	private function help()
	{
		include template('admin/help');
		exit;
	}
}